import { bizApi } from '@/api/biz';
import { Modal } from 'ant-design-vue';

interface ModalConfirmParams {
  title: string;
  action: string;
  content?: string;
  params?: Record<string, any>;
}
/**
 * 通用二次确认组件，点击确认后将发起 post 请求
 * @returns Promise<boolean>
 */
export async function modalConfirm({ title, content, action, params = {} }: ModalConfirmParams) {
  return new Promise<boolean>((resolve, reject) => {
    Modal.confirm({
      title: title,
      content: content || '',
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        try {
          await bizApi.httpSave(action, params);
          resolve(true);
        } catch (error: any) {
          reject(error);
        }
      },
      onCancel: () => {
        resolve(false);
      },
    });
  });
}
